package cn.wolfcode.web.controller;


import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.feign.AlipayFeignApi;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;


@RestController
@RequestMapping("/orderPay")
@RefreshScope
public class OrderPayController {
    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private AlipayFeignApi alipayFeignApi;



    @RequestMapping("/alipay")
    public Result<String> alipay(@RequestParam("orderNo") String orderNo,@RequestParam("type") int type) {
        if(StringUtils.isEmpty(orderNo)||StringUtils.isEmpty(type+"")){
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        //在线支付
        if(OrderInfo.PAYTYPE_ONLINE.equals(type)){
            String form = orderInfoService.payOnline(orderNo);
            return Result.success(form);
        }
        //积分支付
        else if (OrderInfo.PAYTYPE_INTERGRAL.equals(type)) {
            //调用积分支付
            orderInfoService.payIntergral(orderNo);



        }else {
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        return Result.success("");

    }


    @Value("${pay.frontEndPayUrl}")
    private String frontEndPayUrl;

    @RequestMapping("/return_url")
    public void returnUrl(HttpServletResponse response,@RequestParam Map<String,String> params) throws IOException {

        System.out.println(params);
        Result<Boolean> booleanResult=alipayFeignApi.rsaCheck(params);

        if (booleanResult!=null&&!booleanResult.hasError()){

            String orderNo = params.get("out_trade_no");
            if(booleanResult.getData()) {
                response.sendRedirect(frontEndPayUrl+ orderNo);
            }else {
                response.sendRedirect("http://localhost/50x.html");
            }
        }
        //——请在这里编写您的程序（以下代码仅作参考）——



    }
    @RequestMapping("/notify_url")
    public void notifyUrl(HttpServletResponse response,@RequestParam Map<String,String> params) throws IOException {
        PrintWriter writer = response.getWriter();
        Result<Boolean> booleanResult=alipayFeignApi.rsaCheck(params);
        if (booleanResult!=null&&!booleanResult.hasError()){

            String orderNo = params.get("out_trade_no");
            if(booleanResult.getData()) {
                orderInfoService.paySuccess(orderNo);
                writer.write("success");
            }else {
                writer.write("failed");
            }
        }
    }
    @RequestMapping("/refund")
    public Result<String> refund(String orderNo) {
        if (StringUtils.isEmpty(orderNo)) {
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        OrderInfo orderInfo = orderInfoService.find(orderNo);
        if(orderInfo==null){
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        if(OrderInfo.PAYTYPE_ONLINE.equals(orderInfo.getPayType())){
            //在线支付
            orderInfoService.refundOnline(orderNo);

        }else if(OrderInfo.PAYTYPE_INTERGRAL.equals(orderInfo.getPayType())){
            //积分支付
            orderInfoService.refybdIntergral(orderNo);
        }else {
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        return Result.success("");
    }

}
